package cm.kafka.entity;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  日志处理过程数据传输类型
 */
@Data
public class EventLog {

    private static final Logger logger = LoggerFactory.getLogger(EventLog.class);

    private String id;
    //事件接收时间
    private Date EventAcceptTime;
    //用户名称
    private String username;
    //源地址
    private String sourceAddress;
    //源端口
    private Integer sourcePort;
    //操作
    private String operation;
    //目的端口
    private Integer destinationPort;
    // 目的地址
    private String destinationAddress;
    //对象
    private String object;
    //结果
    private String result;
    //持续时间
    private Integer duration;
    //响应时间
    private String response;
    //发送流量
    private Long sendTraffic;
    // 接收流量
    private Long receiveTraffic;
    // 归并数目
    private Integer mergeCount;
    //事件名称
    private String eventName;
    //事件摘要
    private String eventDiest;
    //事件分类
    private String eventType;
    //采集类型
    private String collectionType;
    //等级
    private String level;
    //原始等级
    private String originLevel;
    //原始事件分类
    private String originType;
    //产生时间
    private Date occurTime;// = new Date();
    //监控数值
    private Double monitorValue;
    //网络协议
    private String protocol;
    //网络应用协议
    private String applicationProtocol;
    //源转换ip地址
    private String sourceTransferAddress;
    //源名称
    private String sourceName;
    //源MAC地址
    private String sourceMac;
    //源转换端口
    private Integer sourceTransferPort;
    //目的名称
    private String destinationName;
    //目的MAC地址
    private String destinationMac;
    //目的转换ip地址
    private String destinationTrasferAddress;
    //目的转换端口
    private Integer destinationTrasferPort;
    //设备地址
    private String deviceAddress;
    //设备名称
    private String deviceName;
    //设备类型
    private String deviceType;
    //程序名称
    private String program;
    //采集器ip地址
    private String collectionAddress;
    //请求内容
    private String requestMessage;
    //原始消息
    private String eventMessage;

    //备用长整型1
    private Long extendLong1;
    //备用长整型2
    private Long extendLong2;
    //备用长整型3
    private Long extendLong3;
    //备用长整型4
    private Long extendLong4;
    //备用长整型5
    private Long extendLong5;
    //备用长整型6
    private Long extendLong6;

    //备用整型1
    private String extendInteger1;
    //备用整型2
    private String extendInteger2;
    //备用整型3
    private String extendInteger3;
    //备用整型4
    private String extendInteger4;
    //备用整型5
    private String extendInteger5;
    //备用整型6
    private String extendInteger6;

    //备用字符串1
    private String extendString1;
    //备用字符串2
    private String extendString2;
    //备用字符串3
    private String extendString3;
    //备用字符串4
    private String extendString4;
    //备用字符串5
    private String extendString5;
    //备用字符串6
    private String extendString6;
    //备用字符串7
    private String extendString7;
    //备用字符串8
    private String extendString8;
    //备用字符串9
    private String extendString9;
    //备用字符串10
    private String extendString10;
    //备用字符串11
    private String extendString11;
    //备用字符串12
    private String extendString12;


    //备用双精度1
    private String extendDouble1;
    //备用双精度2
    private String extendDouble2;
    // ---------------------------------------------------分割线------------------------------------------------------//

    // 日志来源主机
    private String host;

    // 日志类型
    private String type;

    // 日志严重程度
    private Integer severity;

    // 日志消息主体
    private String message;

    // logstash监听端口
    private Integer port;

    // 日志产生时间字符串
    private String occurTimeStr;

    // 日志产生时间时间戳
    private String occurTimeStamp;

    private String destinationPortStr;

    public void dateTransferAndSet(){

        String dateFormatter = "yyyy-MM-dd HH:mm:ss";
        if(!StringUtils.isEmpty(occurTimeStr)){
            int length = dateFormatter.length();
            SimpleDateFormat formatter = new SimpleDateFormat(dateFormatter);
            try {
                occurTimeStr = occurTimeStr.substring(0,length);
                Date date = formatter.parse(occurTimeStr);
                this.occurTime  = date;
            }catch (Exception e){
                logger.error("时间格式解析错误：{}",toString(),e);
            }
        }
    }

    public void dateStampTransferDate(){

        String dateFormatter = "yyyy-MM-dd HH:mm:ss";
        if(!StringUtils.isEmpty(occurTimeStamp)){
            if(occurTimeStamp.length()==10){
                occurTimeStamp = occurTimeStamp.concat("000");
            }else if(occurTimeStamp.length()==13){
            }else {
                logger.info("日志范式化产生时间检查:" + this.occurTimeStamp + ",device:"+this.deviceAddress);
            }
            Long dateStamp = Long.valueOf(occurTimeStamp);
            SimpleDateFormat formatter = new SimpleDateFormat(dateFormatter);
            try {
                Date date = formatter.parse(formatter.format(new Date(dateStamp)));
                this.occurTime  = date;
            }catch (Exception e){
                logger.error("时间格式解析错误：{}",toString(),e);
            }
        }
    }


    public void setTransferData(){
        if(StringUtils.isEmpty(level)){
            if(StringUtils.isEmpty(originLevel)){
                this.level = this.originType;
            }else {
                this.level = this.originLevel;
            }
        }
        if(StringUtils.isEmpty(eventType)){
            this.eventType = this.originType;
        }
    }

    public void initData(){
        if(StringUtils.isEmpty(level)){
            this.level = "LOW";
        }
        if(StringUtils.isEmpty(mergeCount)){
            this.mergeCount = 1;
        }
        if(StringUtils.isEmpty(occurTime)){
            this.occurTime = getEventAcceptTime();
        }
        if("0.0.0.0".equals(sourceAddress)){
            this.sourceAddress = deviceAddress;
        }
    }

    public void splitTransferAndSet(String split){
        if(!StringUtils.isEmpty(destinationPortStr) && !StringUtils.isEmpty(split)){
            String[] splitStrings = destinationPortStr.split(split);
            this.destinationPort = Integer.valueOf(splitStrings[0]);
        }
    }

    @Override
    public String toString() {
        return "EventLog{" +
                "EventAcceptTime=" + EventAcceptTime +
                ", username='" + username + '\'' +
                ", sourceAddress='" + sourceAddress + '\'' +
                ", sourcePort=" + sourcePort +
                ", operation='" + operation + '\'' +
                ", destinationPort=" + destinationPort +
                ", destinationAddress='" + destinationAddress + '\'' +
                ", object='" + object + '\'' +
                ", result='" + result + '\'' +
                ", duration=" + duration +
                ", response=" + response +
                ", sendTraffic=" + sendTraffic +
                ", receiveTraffic=" + receiveTraffic +
                ", mergeCount=" + mergeCount +
                ", eventName='" + eventName + '\'' +
                ", eventDiest='" + eventDiest + '\'' +
                ", eventType='" + eventType + '\'' +
                ", collectionType='" + collectionType + '\'' +
                ", level='" + level + '\'' +
                ", originLevel='" + originLevel + '\'' +
                ", originType='" + originType + '\'' +
                ", occurTime=" + occurTime +
                ", monitorValue=" + monitorValue +
                ", protocol='" + protocol + '\'' +
                ", applicationProtocol='" + applicationProtocol + '\'' +
                ", sourceTransferAddress='" + sourceTransferAddress + '\'' +
                ", sourceName='" + sourceName + '\'' +
                ", sourceMac='" + sourceMac + '\'' +
                ", sourceTransferPort=" + sourceTransferPort +
                ", destinationName='" + destinationName + '\'' +
                ", destinationMac='" + destinationMac + '\'' +
                ", destinationTrasferAddress='" + destinationTrasferAddress + '\'' +
                ", destinationTrasferPort=" + destinationTrasferPort +
                ", deviceAddress='" + deviceAddress + '\'' +
                ", deviceName='" + deviceName + '\'' +
                ", deviceType='" + deviceType + '\'' +
                ", program='" + program + '\'' +
                ", collectionAddress='" + collectionAddress + '\'' +
                ", requestMessage='" + requestMessage + '\'' +
                ", eventMessage='" + eventMessage + '\'' +
                ", host='" + host + '\'' +
                ", type='" + type + '\'' +
                ", severity=" + severity +
                ", message='" + message + '\'' +
                ", port=" + port +
                ", occurTimeStr='" + occurTimeStr + '\'' +
                '}';
    }

}
